package com.hxl.tech.gateway.auth.dao;


import com.hxl.tech.gateway.auth.dto.openapi.OpenApiFindDTO;
import com.hxl.tech.gateway.auth.model.OpenApi;
import com.hxl.tech.gateway.auth.model.QOpenApi;
import com.querydsl.jpa.impl.JPAQuery;
import com.querydsl.jpa.impl.JPAQueryFactory;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;


@Repository
public class OpenApiDao {

    @Resource
    private JPAQueryFactory jpaQueryFactory;

    private QOpenApi qOpenApi = QOpenApi.openApi;

    public OpenApi read(String apiName) {

        JPAQuery<OpenApi> query = jpaQueryFactory.select(qOpenApi)
                .from(qOpenApi)
                .where(qOpenApi.apiName.eq(apiName));
        return query.fetchOne();
    }

    public List<OpenApi> find(OpenApiFindDTO findDTO) {

        JPAQuery<OpenApi> query = jpaQueryFactory.select(qOpenApi).from(qOpenApi);
        if (StringUtils.isNotEmpty(findDTO.getApiModule())) {
            query.where(qOpenApi.apiModule.eq(findDTO.getApiModule()));
        }
        return query.fetch();
    }
}
